package com.itheima.service.impl;


import cn.itheima.health.entity.QueryPageBean;
import cn.itheima.health.service.MenuAdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.mapper.MenuAdminMapper;
import com.itheima.mapper.RoleAdminMapper;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class MenuAdminServiceImpl implements MenuAdminService {

    @Autowired
    private MenuAdminMapper menuAdminMapper;

    @Autowired
    private RoleAdminMapper roleAdminMapper;

    @Override
    public Map findPage(QueryPageBean queryPageBean) {

        Map map = new HashMap();
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Menu> list = menuAdminMapper.findPage(queryPageBean.getQueryString());
        if (list != null && list.size() > 0) {
            for (Menu menu : list) {
                Integer id = menu.getId();
                List<Integer> roleIds = menuAdminMapper.finRoleAndMenuById(id);
                if (roleIds != null && list.size() > 0) {
                    for (Integer roleId : roleIds) {
                        String name = menuAdminMapper.findRoleName(roleId);
                        Set<Role> roles = new HashSet<>();
                        Role role = new Role();
                        role.setName(name);
                        roles.add(role);
                        menu.setRoles(roles);
                    }
                }
            }
        }
        PageInfo<Menu> pageInfo = new PageInfo<>(list);
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return map;
    }

    @Override
    public void add(Menu menu, Integer[] roleIds) {

        menuAdminMapper.add(menu);

        Integer menuId = menu.getId();

        if(roleIds!=null){
            for (Integer roleId : roleIds) {
                menuAdminMapper.addMenuAndRole(roleId,menuId);
            }
        }
    }

    @Override
    public Map findRoleIdsByMenuId(Integer id) {
        Map map = new HashMap();
        List<Role> list = roleAdminMapper.findRoleAll();
        Integer[] roleIds = menuAdminMapper.findRoleIdsByMenuId(id);
        map.put("list",list);
        map.put("roleIds",roleIds);
        return map;
    }

    @Override
    public void edit(Menu menu, Integer[] roleIds) {

        menuAdminMapper.edit(menu);

        Integer menuId = menu.getId();

        menuAdminMapper.removeRoleAndMenuByMenuId(menuId);

        if(roleIds!=null){
            for (Integer roleId : roleIds) {
                menuAdminMapper.addMenuAndRole(roleId,menuId);
            }
        }
    }

    @Override
    public void deleteMenuAdminById(Integer id) {
        menuAdminMapper.removeRoleAndMenuByMenuId(id);
        menuAdminMapper.deleteMenuAdminById(id);
    }
}
